package com.riant.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Classname MyConstrainValidator
 * @Description TODO
 * @Date 2022/5/2 2:31
 * @Created by 张斌
 */
public class MyConstrainValidator implements ConstraintValidator<MyConstraint, Object> {

    private int min;
    private int max;
    @Override
    public void initialize(MyConstraint constraintAnnotation) {
        this.max = constraintAnnotation.max();
        this.min = constraintAnnotation.min();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (!(value instanceof Integer)) {
            return false;
        }
        int temp = (Integer) value;
        if (temp < min || temp > max) {
            return false;
        }
        return true;
    }
}
